#include "mainwidget.h"
#include "./ui_mainwidget.h"


#include <QHBoxLayout> // 按水平方向，从左往右排列部件
#include <QVBoxLayout> // 按竖直方向，从上往下排列部件
#include <QIcon>
#include <QGridLayout> // “格子”布局
#include "debug.h"
#include "sessiondetailwidget.h"


MainWidget* MainWidget::instance = nullptr;

MainWidget* MainWidget::GetInstance()
{
    if (instance == nullptr)
    {
        instance = new MainWidget();
    }
    return instance;
}

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("聊天室");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));

    InitMainWin();
    InitLeftWin();
    InitMidWin();
    InitRightWin();

    InitSignalSlot(); // 初始化信号槽
}

void MainWidget::InitMainWin()
{
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setSpacing(0); // 设置布局中各部件的间隔距离
    layout->setContentsMargins(0, 0, 0, 0); // 设置边界距离

    // setLayout
    this->setLayout(layout);

    // Left, Mid, Right QWidget
    win_left = new QWidget();
    win_mid = new QWidget();
    win_right = new QWidget();

    // Left, Mid 设置定宽; Right 设置变宽
    win_left->setFixedWidth(53);
    win_mid->setFixedWidth(250);
    win_right->setMinimumWidth(600);

    // 设置三部分样式
    win_left->setStyleSheet("QWidget { background-color: rgb(46, 46, 46); }");
    win_mid->setStyleSheet("QWidget { background-color: rgb(247, 247, 247); }");
    win_right->setStyleSheet("QWidget { background-color: rgb(245, 245, 245); }");

    // addWidget
    layout->addWidget(win_left);
    layout->addWidget(win_mid);
    layout->addWidget(win_right);
}

void MainWidget::InitLeftWin()
{
    // 1. 创建一个纵置布局
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 40, 0, 0);
    layout->setSpacing(30); // 设置各部件间隔距离

    win_left->setLayout(layout);

    // 2. 添加用户头像
    user_avatar_btn = new QPushButton();
    user_avatar_btn->setFixedSize(QSize(42, 42));
    user_avatar_btn->setIconSize(QSize(42, 42));
    user_avatar_btn->setIcon(QIcon(":/resource/image/myAvatar.jpg"));
    user_avatar_btn->setStyleSheet("QPushButton { border:none; background-color: transparent; }");
    // 占据一行一列，定格对齐和水平居中
    layout->addWidget(user_avatar_btn, 1, Qt::AlignTop | Qt::AlignHCenter);

    // 3. 添加会话列表
    session_tab_btn = new QPushButton();
    session_tab_btn->setFixedSize(QSize(23, 23));
    session_tab_btn->setIconSize(QSize(23, 23));
    session_tab_btn->setIcon(QIcon(":/resource/image/session_active.png"));
    session_tab_btn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    layout->addWidget(session_tab_btn, 1, Qt::AlignTop | Qt::AlignHCenter);

    // 4. 添加好友列表
    friend_tab_btn = new QPushButton();
    friend_tab_btn->setFixedSize(QSize(23, 23));
    friend_tab_btn->setIconSize(QSize(23, 23));
    friend_tab_btn->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    friend_tab_btn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    layout->addWidget(friend_tab_btn, 1, Qt::AlignTop | Qt::AlignHCenter);

    // 5. 添加一个伸缩因子
    layout->addStretch(20);
}

void MainWidget::InitMidWin()
{
    // 1. 创建布局管理器，设置相关间距
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0, 20, 0, 0);
    layout->setHorizontalSpacing(0); // 列部件之间间隔
    layout->setVerticalSpacing(11);  // 行部件之间间隔

    win_mid->setLayout(layout);

    // 2. 初始化好友搜索框
    search_box = new QLineEdit();
    // search_box->setFixedWidth(160);
    search_box->setFixedHeight(25);
    search_box->setPlaceholderText(QString(" 搜索"));
    search_box->setStyleSheet("QLineEdit { border-radius: 5px; background-color: rgb(226, 226, 226); }");

    // 3. 初始化好友添加按钮
    friends_add_btn = new QPushButton();
    friends_add_btn->setFixedWidth(25);
    friends_add_btn->setFixedHeight(25);
    friends_add_btn->setIcon(QIcon(":/resource/image/add.png"));
    QString style = "QPushButton { border-radius: 5px; background-color: rgb(226, 226, 226); }";
    style += "QPushButton:pressed { border-radius: 5px; background-color: rgb(209, 209, 209); } ";
    friends_add_btn->setStyleSheet(style);

    // 4. 创建三个相同间距的空间，分隔 搜索框 和 添加按钮
    QWidget* space_1 = new QWidget();
    QWidget* space_2 = new QWidget();
    QWidget* space_3 = new QWidget();
    space_1->setFixedWidth(8);
    space_2->setFixedWidth(8);
    space_3->setFixedWidth(8);

    // 5. 初始化 会话/好友-区
    session_friend_area = new SessionFriendArea();

    // 6. 设置布局
    layout->addWidget(space_1, 0, 0, 1, 1);
    layout->addWidget(search_box, 0, 1, 1, 1);
    layout->addWidget(space_2, 0, 2, 1, 1);
    layout->addWidget(friends_add_btn, 0, 3, 1, 1);
    layout->addWidget(space_3, 0, 4, 1, 1);
    layout->addWidget(session_friend_area, 1, 0, 1, 5); // 第2行第1列，占据 1行 5列
}

void MainWidget::InitRightWin()
{
    // 1. 设置纵置布局
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setSpacing(0);
    vlayout->setAlignment(Qt::AlignTop);
    win_right->setLayout(vlayout);

    // win_right->setStyleSheet("QWidget { border-left: 1px solid rgb(214, 214, 214); }");

    // 2. 会话标题栏
    QWidget* session_title = new QWidget();
    session_title->setStyleSheet("QWidget { background-color: rgb(245, 245, 245); border-bottom: 1px solid rgb(231, 231, 231);}");
    session_title->setFixedHeight(58);
    session_title->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // session_title->setObjectName("Session_Title");
    // session_title->setStyleSheet("#Session_Title { border-bottom: 1px solid rgb(231, 231, 231); }");

    vlayout->addWidget(session_title);

    // 2.2 添加会话标题
    QHBoxLayout* title_layout = new QHBoxLayout();
    title_layout->setContentsMargins(14, 0, 6, 0);
    title_layout->setSpacing(0);
    session_title->setLayout(title_layout);

    QLabel* session_label = new QLabel();
    QString label_style = "QLabel { background-color: transparent; }";
    label_style += "QLabel { font-size: 18px; color: rgb(0, 0, 0); }";
    session_label->setStyleSheet(label_style);

    session_label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    title_layout->addWidget(session_label);
#if UI_TEST
    session_label->setText("测试会话标签");
#endif

    // 2.3 添加 '更多' 按钮
    more_btn = new QPushButton();
    more_btn->setFixedSize(QSize(22, 22));
    more_btn->setIconSize(QSize(22, 22));
    more_btn->setIcon(QIcon(":/resource/image/more.png"));
    QString more_btn_style = "QPushButton { border-radius: 5px; border: none; background-color: transparent; }";

    // 等待改善
    more_btn_style += "QPushButton::pressed { background-color: rgb(209, 209, 209); }";
    more_btn->setStyleSheet(more_btn_style);

    title_layout->addWidget(more_btn);

    // 3. 消息展示区
    message_display_area = new MessageDisplayArea();
    vlayout->addWidget(message_display_area);

    // 4. 消息发送区
    message_sending_area = new MessageSendingArea();
    vlayout->addWidget(message_sending_area, 0, Qt::AlignBottom);
}

void MainWidget::SwitchToSession()
{
    // 1. 记录当前切换到的标签页
    activetab = SESSION_LIST;
    // 2. 图标变换
    session_tab_btn->setIcon(QIcon(":/resource/image/session_active.png"));
    friend_tab_btn->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    // 3. 加载对应信息
}

void MainWidget::SwitchToFriend()
{
    // 1. 记录当前切换到的标签页
    activetab = FRIEND_LIST;
    // 2. 图标变换
    session_tab_btn->setIcon(QIcon(":/resource/image/session_inactive.png"));
    friend_tab_btn->setIcon(QIcon(":/resource/image/friend_active.png"));
    // 3. 加载对应信息
}

void MainWidget::InitSignalSlot()
{
    // 发出的信号 点击按钮时发出的信号 接收信号的对象 信号被触发时执行的函数
    connect(session_tab_btn, &QPushButton::clicked, this, &MainWidget::SwitchToSession);
    connect(friend_tab_btn, &QPushButton::clicked, this, &MainWidget::SwitchToFriend);

    // 点击会话详情按钮，弹出“会话详情”窗口
    connect(more_btn, &QPushButton::clicked, this, [=](){

        SessionDetailWidget* detail_widget = new SessionDetailWidget(this, UserInfo());
        detail_widget->exec();
    });
}

MainWidget::~MainWidget()
{
    delete ui;
}
